package com.yaodian100.mall.member.web;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.captcha.Captcha;
import nl.captcha.Captcha.Builder;
import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.gimpy.FishEyeGimpyRenderer;
import nl.captcha.noise.CurvedLineNoiseProducer;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.producer.DefaultTextProducer;
import nl.captcha.text.renderer.DefaultWordRenderer;
import nl.captcha.text.renderer.WordRenderer;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.member.entity.ImageValid;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.mall.member.util.UserData;

/**
 * Servlet implementation class ImageValid
 */
public class ImageValidServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private MemberService memberService;
	private ServletContext servletContext;

	@Override
	public void init() throws ServletException {
		servletContext = getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		this.memberService = (MemberService) wac.getBean("memberService");
	}

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ImageValidServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public Color getRandColor(int fcolor, int bcolor) {
		Random random = new Random();
		if (fcolor > 255) {
			fcolor = 255;
		}
		if (bcolor > 255) {
			bcolor = 255;
		}
		int r = fcolor + random.nextInt(bcolor - fcolor);
		int g = fcolor + random.nextInt(bcolor - fcolor);
		int b = fcolor + random.nextInt(bcolor - fcolor);
		return new Color(r, g, b);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			Random random = new Random();

			String msg = "0123456789";
			List<Font> fontList = new ArrayList<Font>();
			List<Color> colorList1 = new ArrayList<Color>();
			List<Color> colorList2 = new ArrayList<Color>();
			colorList1.add(new Color(96, 0, 145));
			colorList1.add(new Color(7, 2, 209));
			colorList1.add(new Color(208, 1, 118));
			colorList1.add(new Color(1, 150, 5));
			colorList1.add(new Color(153, 0, 0));

			fontList.add(new Font("Arial", Font.PLAIN, 32));
			int colorRanNum = random.nextInt(5);
			colorList2.add(colorList1.get(colorRanNum));
			DefaultWordRenderer dwr = new DefaultWordRenderer(colorList2, fontList);
			WordRenderer wr = dwr;

			GradiatedBackgroundProducer gbp = new GradiatedBackgroundProducer();

			Builder builder = new Captcha.Builder(110, 40);
			builder.addText(new DefaultTextProducer(6, msg.toCharArray()), wr);

			List<Color> bgColorList = new ArrayList<Color>();
			bgColorList.add(new Color(204, 204, 204));
			bgColorList.add(new Color(235, 216, 193));
			bgColorList.add(new Color(252, 224, 243));

			int bgRanNum = random.nextInt(3);
			gbp.setFromColor(bgColorList.get(bgRanNum));
			gbp.setToColor(bgColorList.get(bgRanNum));

			int ranNum = random.nextInt(3);
			if (ranNum == 0) {// #cccccc
				builder.addNoise(new CurvedLineNoiseProducer(Color.RED, 1.2f));
				builder.addNoise(new CurvedLineNoiseProducer(Color.BLUE, 1f));
				builder.addNoise(new CurvedLineNoiseProducer(Color.YELLOW, 0.8f));
			} else if (ranNum == 1) {// #ebd8c1
				List<Color> fishEyeColorList = new ArrayList<Color>();
				fishEyeColorList.add(new Color(255, 219, 52));// 黃
				fishEyeColorList.add(new Color(115, 105, 197));// 紫
				fishEyeColorList.add(new Color(255, 14, 150));// 桃紅
				fishEyeColorList.add(new Color(52, 176, 255));// 亮藍
				fishEyeColorList.add(new Color(95, 255, 25));// 亮綠
				int fishEyeRanNum = random.nextInt(5);
				builder.gimp(new FishEyeGimpyRenderer(Color.lightGray, fishEyeColorList.get(fishEyeRanNum)));
			} else {// #fce0f3
				builder.addNoise();
			}
			// 创建对象
			builder.addBackground(gbp);
			Captcha captcha = builder.build();

			ImageValid imageValid = new ImageValid();
			imageValid.setValidData(captcha.getAnswer());
			imageValid = memberService.saveImageValid(imageValid);
			UserData userData = new UserData(request, response);
			userData.setImageValid(imageValid.getId());
			CaptchaServletUtil.writeImage(response, captcha.getImage());
			response.flushBuffer();
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

}